﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace assignment5
{
    public class OrderService
    {
        private List<OrderDetails> orderDetails=new List<OrderDetails>();

        public OrderService(OrderDetails od)
        {
            orderDetails.Add(od);
        }
        public void AddOder()
        {
            OrderDetails odd = Create();
            foreach (var item in orderDetails)
            {
                if (item.Equals(odd))
                {
                    Console.WriteLine("订单重复");
                    return;
                }
            }
            orderDetails.Add(odd);            
        }
        public OrderDetails find_id(int num)
        {
            foreach (var item in orderDetails)
            {
                if (num == item.Getid)
                {
                    return item;
                }
            }
            Console.WriteLine($"没有订单号为{num}的订单");
            return null;
        }
        public void  Deloder(int num)
        {
            orderDetails.Remove(find_id(num));
        }
        public void Reoder(int num)
        {
            int index = orderDetails.IndexOf(find_id(num));
            if (index != -1)
            {
                orderDetails[index]= Create();
            }
            else
            {
                Console.WriteLine(" 未找到订单！");
            }

        }
        public void Reserch(int num)
        {
            int index = orderDetails.IndexOf(find_id(num));
            if (index != -1)
            {
                Console.WriteLine(orderDetails[index].ToString());
            }
            else
            {
                Console.WriteLine(" 未找到订单！");
            }
        }
        public void Findupp(int le)
        {
            var x = from n in orderDetails
                    where n.Total >= le 
                    select n;
            foreach (var item in x)
            {
                Console.WriteLine(item.ToString());
            }
        }
        public void Sortoder()
        {
            for(int i = 0; i < orderDetails.Count; i++)
            {
                for(int j = i;j < orderDetails.Count; j++)
                {
                    if (orderDetails[i].Getid > orderDetails[j].Getid)
                    {
                        OrderDetails od = orderDetails[i];
                        orderDetails[i] = orderDetails[j];
                        orderDetails[j] = od;
                    }
                }
            }
            foreach (var item in orderDetails)
            {
                Console.WriteLine(item.ToString());
            }
        }
        public OrderDetails Create()
        {
            int total;
            List<(int, int, string)> goods = new List<(int, int, string)>();
            Console.WriteLine("输入订单号：");
            string temp = Console.ReadLine();
            if (!int.TryParse(temp, out int id))
            {
                Console.WriteLine("输入的订单号格式不对！");
            }

            Console.WriteLine("请输入客户名字");
            string name = Console.ReadLine();

            Console.Write("请输入一个时间（格式：yyyy-MM-dd HH:mm）：");
            string input = Console.ReadLine();
            DateTime time= new DateTime();

            try
            {
                time = DateTime.ParseExact(input, "yyyy-MM-dd HH:mm", null);
                Console.WriteLine($"你输入的时间是：{time.ToString("yyyy-MM-dd HH:mm")}");
            }
            catch (FormatException)
            {
                Console.WriteLine("输入的时间格式不正确！");
            }

            Console.WriteLine("输入商品名称：");
            string goodsname = Console.ReadLine();
            Console.WriteLine("输入商品数量：");
            string a = Console.ReadLine();
            if (!int.TryParse(a, out int counts))
            {
                Console.WriteLine("输入的数量格式不对");
            }
            Console.WriteLine("输入单件商品价格：");
            string b = Console.ReadLine();
            if (!int.TryParse(b, out int price))
            {
                Console.WriteLine("输入价格格式不对");
            }
            if (goodsname != null)
                goods.Add((price, counts, goodsname));
            else goods.Add((price, counts, " "));
            Order od = new Order(id, name);
            OrderDetails oddetails = new OrderDetails(od, time, goods);
            return oddetails;
        }

    }
}
